package pers.zc.activiti.scheduling;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @description 任务调度配置
 * @Author Lin FuYuan
 * @date 2020/4/2
 */
@Configuration
public class SchedulingConfig  implements SchedulingConfigurer {
  /**
   *配置线程池
   * @author Lin FuYuan
   * @param taskRegistrar
   * @date 2020/4/2
   * @return void
   */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = taskScheduler();
        taskRegistrar.setScheduler(threadPoolTaskScheduler);
    }
    /**
     * 线程池参数配置
     * @author Lin FuYuan
     * @param
     * @date 2020/4/2
     * @return org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
     */
    @Bean(destroyMethod = "shutdown")
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        scheduler.setThreadNamePrefix("Scheduling-");
        scheduler.setAwaitTerminationSeconds(600);
        scheduler.setErrorHandler(throwable -> System.out.println("调度任务发生异常"));
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        return scheduler;
    }
}
